<template>
  <div>
    <div class="toolbar">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchShopTypeName" placeholder="店铺类别"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="$store.dispatch('getShopTypeList',{shopTypeName:searchShopTypeName})"
          >查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="shopTypeVisible=true">添加店铺类别</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :data="$store.state.shop.shopTypeList"
      :border="true"
      v-loading="$store.state.isLoading"
      style="width: 100%"
    >
      <el-table-column label="ID" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row._id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传时间" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime|date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" width="180">
        <template slot-scope="scope">{{scope.row.upTime|date}}</template>
      </el-table-column>
      <el-table-column label="店铺类别名称" width="180">
        <template slot-scope="scope">{{scope.row.shopTypeName}}</template>
      </el-table-column>
      <el-table-column label="店铺类别图片" width="180">
        <template slot-scope="scope">
          <a :href="scope.row.shopTypePic | imgUrl" :title="scope.row.shopTypeName" target="_blank">
            <img height="100px" :src="scope.row.shopTypePic | imgUrl" alt />
          </a>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="shopTypeId=scope.row._id;shopVisible=true"
          >添加店铺</el-button>
          <el-button size="mini" type="danger" @click="initShopTypeInfo(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="$store.dispatch('deleteShopList',scope.row._id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <PageInfo action-name="getShopTypeList" :params="{shopTypeName:searchShopTypeName}"></PageInfo>

    <el-dialog title="添加店铺类别" :visible.sync="shopTypeVisible">
      <el-form>
        <el-form-item label="店铺类别名称:" label-width="120px">
          <el-input v-model="shopTypeForm.shopTypeName" autocomplete="off" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="店铺类别图片:" label-width="120px">
          <el-upload
            name="shopTypePic"
            :data="shopTypeForm"
            ref="upload"
            :limit="1"
            :on-success="success"
            :auto-upload="false"
            class="upload-demo"
            action="/api/shopTypeList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="shopTypeVisible=false">取 消</el-button>
        <el-button type="primary" @click="addShopType">提 交</el-button>
      </div>
    </el-dialog>
    <ShopDialog v-if="shopVisible" :shopVisible.sync="shopVisible" :shopTypeId="shopTypeId"></ShopDialog>
  </div>
</template>

<script>
import ShopDialog from "@/components/dialog/ShopDialog";

export default {
  components: {
    ShopDialog
  },
  props: {},
  data() {
    return {
      shopTypeId: "",
      shopVisible: false,
      searchShopTypeName: "",
      shopTypeVisible: false,
      shopTypeForm: {
        shopTypeName: ""
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    async addShopType() {
      if (this.shopTypeId) {
        const formdata = new FormData();
        // 添加一个shopTypeId  key
        formdata.set("shopTypeId", this.shopTypeId);
        formdata.set("shopTypeName", this.shopTypeForm.shopTypeName);
        formdata.set(
          "shopTypePic",
          document.querySelector(".el-upload__input").files[0]
        );
        const result = await this.$http.put("/shopTypeList", formdata);
        console.log(result);
        this.success(result);
      } else {
        this.$refs.upload.submit();
      }
    },
    initShopTypeInfo(shopTypeInfo) {
      this.shopTypeId = shopTypeInfo._id;
      this.shopTypeForm.shopTypeName = shopTypeInfo.shopTypeName;
      this.fileList = [
        {
          name: shopTypeInfo.shopTypePic,
          url: "/api/" + shopTypeInfo.shopTypePic
        }
      ];
      this.shopTypeVisible = true;
    },
    success(data, file, fileList) {
      console.log(data, file, fileList);
      if (data.ok === 1) {
        this.shopTypeVisible = false;
        this.$store.dispatch("getShopTypeList");
      } else {
        alert(this.msg);
      }
    }
  },
  created() {
    if (this.shopTypeId) {
      this.shopForm.shopTypeId = this.shopTypeId;
    }
  },
  mounted() {
    if (this.$store.state.shop.allShopTypeList.length < 1) {
      this.$store.dispatch("allShopTypeList");
    }
  }
};
</script>
<style lang="scss" scoped>
</style>